package com.zengfa.task.server.domain.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zengfa.task.api.enums.JobStatus;
import com.zengfa.task.server.domain.entity.Job;
import com.zengfa.task.server.domain.entity.JobNode;
import com.zengfa.task.server.domain.service.IJobNodeService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class JobNodeServiceImpl implements IJobNodeService{

	@Override
	public void saveTaskNodes(Job job, List<JobNode> nodeList) {
		/*
		int i = 0;
		for(JobNode node : nodeList) {
			JobNode jobNode = findByNodeCode(job.getJobId(), node.getNodeCode());
			if(jobNode != null) {
				jobNode.setNodeName(node.getNodeName());
				jobNode.setNodeOrder(++i);
				jobNode.setRunType(node.getRunType());
				jobNodeMapper.updateById(jobNode);
				continue;
			}
			jobNode = new JobNode();
			jobNode.setJobId(job.getJobId());
			jobNode.setNodeCode(node.getNodeCode());
			jobNode.setNodeName(node.getNodeName());
			jobNode.setNodeOrder(++i);
			jobNode.setRunType(node.getRunType());
			jobNode.setStatus(TaskConstant.JOB_STATUS_WAIT);
			jobNode.setDeleted(0);
            jobNodeMapper.insert(jobNode);
		}
		*/
	}
	
	@Override
	public JobNode findByNodeCode(String jobId,String nodeCode) {
		/*
		QueryWrapper<JobNode> wrapper = new QueryWrapper<>();
        wrapper.eq("job_id", jobId);
        wrapper.eq("node_code", nodeCode);
        wrapper.eq("deleted",0);
        List<JobNode> list = jobNodeMapper.selectList(wrapper);
        if(list != null && list.size() > 0) {
        	return list.get(0);
        }
        return null;
        */
		return null;
	}
	
	@Override
	public List<JobNode> listByJobId(String jobId) {
		/*
		QueryWrapper<JobNode> wrapper = new QueryWrapper<>();
        wrapper.eq("job_id", jobId);
        wrapper.eq("deleted",0);
        wrapper.orderByAsc("node_order");
		List<JobNode> list = jobNodeMapper.selectList(wrapper);
		return list;
		*/
		return null;
	}
	
	@Override
	public void saveNodeStatus(String nodeId, JobStatus status, String message) {
		/*
		JobNode jobNode = jobNodeMapper.selectById(nodeId);
		if(jobNode == null) {
			return false;
		}
		if(JobUtil.isFinish(status)) {
			jobNode.setEndTime(LocalDateTime.now());
        }else if(TaskConstant.JOB_STATUS_START.equals(status)) {
        	jobNode.setStartTime(LocalDateTime.now());
        }
		if(TaskConstant.JOB_STATUS_START.equals(status)) {
			jobNode.setMessage(null);
		}
		if (StringUtils.isNotBlank(jobNode.getMessage())) {
			message = jobNode.getMessage() + ";" + message;
        }
		jobNode.setMessage(message);
		jobNode.setStatus(status);
		jobNode.setUpdatedDate(LocalDateTime.now());
		
		QueryWrapper<JobNode> wrapper = new QueryWrapper<>();
        wrapper.eq("id", nodeId);
        wrapper.eq("deleted", 0);
        if(TaskConstant.JOB_STATUS_START.equals(status)) {
//        	wrapper.eq("status", TaskConstant.JOB_STATUS_WAIT);
		}else if(TaskConstant.JOB_STATUS_RUNNING.equals(status)) {
			wrapper.eq("status", TaskConstant.JOB_STATUS_START);
		}else if(JobUtil.isFinish(status)) {
			wrapper.in("status", TaskConstant.JOB_STATUS_RUNNING,TaskConstant.JOB_STATUS_WAIT);
		}
        
        int i = jobNodeMapper.update(jobNode, wrapper);
        log.info("更新任务节点状态:{},{},{}",nodeId,status,i);
        if(i > 0) {
        	return true;
        }
        return false;
        */
	}

	@Override
	public void resetJobNodes(String jobId) {
		/*
		List<JobNode> list = listByJobId(jobId);
		for(JobNode r : list) {
			LambdaUpdateWrapper<JobNode> wrapper = new LambdaUpdateWrapper<>();
			wrapper.eq(JobNode::getId, r.getId());
			wrapper.set(JobNode::getStartTime, null);
			wrapper.set(JobNode::getEndTime, null);
			wrapper.set(JobNode::getMessage, "");
			wrapper.set(JobNode::getStatus, TaskConstant.JOB_STATUS_WAIT);
			update(wrapper);
		}
		*/
	}

}
